package id.co.rho.scoring.cid.client.mvc.versioning;

import id.co.rho.scoring.cid.client.helper.Resources;
import id.co.rho.scoring.cid.client.model.CriteriaTree;
import id.co.rho.scoring.cid.client.mvc.AppView;
import id.co.rho.scoring.cid.client.utils.CommonUtils;
import id.co.rho.scoring.cid.client.utils.Folder;

import java.util.Arrays;
import java.util.List;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.ModelIconProvider;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MenuEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.event.WindowEvent;
import com.extjs.gxt.ui.client.event.WindowListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.store.TreeStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.SimpleComboBox;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.extjs.gxt.ui.client.widget.layout.HBoxLayout;
import com.extjs.gxt.ui.client.widget.menu.Menu;
import com.extjs.gxt.ui.client.widget.menu.MenuItem;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.extjs.gxt.ui.client.widget.treegrid.EditorTreeGrid;
import com.extjs.gxt.ui.client.widget.treegrid.TreeGrid;
import com.extjs.gxt.ui.client.widget.treegrid.TreeGridCellRenderer;
import com.extjs.gxt.ui.client.widget.treegrid.TreeGridSelectionModel;
import com.google.gwt.user.client.ui.AbstractImagePrototype;

public class VersioningSetDefaultUIWeb implements VersioningSetDefaultUI{
	private Folder model;
	private TreeStore<ModelData> store;
	private ColumnConfig criteriaName, criteriaNumber, criteriaRank,
			criteriaWeight, criteriaType, id, parentId;
	private ColumnModel cm;
	private TreeGrid<ModelData> tree;
	public ContentPanel cp;
	private ToolBar toolbar;
	private Button btnSetDefault;
	public Long modelId;
	private boolean addData;
	private String tipe;
	private Long idParent;
	private Long idCriteria;

	public void initialize() {

		model = new Folder();

		store = new TreeStore<ModelData>();
		store.add(model.getChildren(), true);

		TreeGridCellRenderer<ModelData> labelColumnRenderer = new TreeGridCellRenderer<ModelData>() {
			@Override
			public Object render(ModelData modelData, String property,
					ColumnData config, int rowIndex, int colIndex,
					ListStore<ModelData> modelDataListStore,
					Grid<ModelData> modelDataGrid) {

				Object result = super.render(modelData, property, config,
						rowIndex, colIndex, modelDataListStore, modelDataGrid);
				config.style = config.style.replaceAll(
						"background-color:[#1-9a-zA-Z]+;", "");
				if (!CommonUtils.isEmpty(modelData.<String> get("changeState"))) {
					if (modelData.<String> get("changeState").equalsIgnoreCase(
							"edit")) {
						config.style = config.style
								+ "background-color:#FF33CC;";
					} else if (modelData.<String> get("changeState")
							.equalsIgnoreCase("new")) {
						config.style = config.style
								+ "background-color:#FFF333;";
					}
				}

				return result;
			}
		};

		criteriaName = new ColumnConfig("criteriaName", "Name", 100);
		criteriaName.setRenderer(labelColumnRenderer);

		criteriaNumber = new ColumnConfig("criteriaNumber", "Number", 100);
		criteriaRank = new ColumnConfig("criteriaRank", "Rank", 100);
		criteriaWeight = new ColumnConfig("criteriaWeight", "Weight", 100);
		criteriaType = new ColumnConfig("criteriaType", "Type", 100);

		id = new ColumnConfig("id", "Id", 100);
		parentId = new ColumnConfig("parentId", "Parent", 100);
		parentId.setHidden(true);
		id.setHidden(true);
		cm = new ColumnModel(Arrays.asList(criteriaName, criteriaNumber,
				criteriaRank, criteriaWeight, criteriaType, id, parentId));
		toolbar = new ToolBar();
		btnSetDefault = new Button("Set as Default");
		toolbar.add(btnSetDefault);

		cp = new ContentPanel();
		cp.setBodyBorder(false);
		cp.setHeading("Scoring Model");
		cp.setButtonAlign(HorizontalAlignment.CENTER);
		cp.setLayout(new FitLayout());
		cp.setFrame(true);
		cp.setSize(600, 300);

		tree = new EditorTreeGrid<ModelData>(store, cm);
		tree.setBorders(true);
		tree.setIconProvider(new ModelIconProvider<ModelData>() {
			@Override
			public AbstractImagePrototype getIcon(final ModelData model) {
				AbstractImagePrototype result = Resources.ICONS.textCriteria();
				if (!CommonUtils.isEmpty(model.<String> get("criteriaType"))) {
					if (model.<String> get("criteriaType").equalsIgnoreCase(
							"answer")) {
						result = Resources.ICONS.answer();
					} else if (model.<String> get("criteriaType")
							.equalsIgnoreCase("question")) {
						result = Resources.ICONS.question();
					}
				}

				return result;
			}
		});
		tree.setSize(400, 400);
		tree.setAutoExpandColumn("criteriaName");
		tree.setTrackMouseOver(false);
		tree.setSelectionModel(new TreeGridSelectionModel<ModelData>());
		tree.expandAll();

		cp.add(tree);
	}

	public void show() {

		BorderLayoutData borderlayoutDataNorth = new BorderLayoutData(
				LayoutRegion.NORTH);
		borderlayoutDataNorth.setSize(25);
		ContentPanel centerPanel = (ContentPanel) Registry
				.get(AppView.CENTER_PANEL);

		centerPanel.removeAll();
		centerPanel.setLayout(new BorderLayout());
		centerPanel.add(toolbar, borderlayoutDataNorth);
		centerPanel.add(cp, new BorderLayoutData(LayoutRegion.CENTER));

		centerPanel.layout();
		tree.expandAll();
	}

	public void setModel(Folder model) {
		this.model = model;
		this.tree.expandAll();
		store.removeAll();
		store.add(model.getChildren(), true);
		cp.layout();
		tree.expandAll();
	}

	public TreeGrid<ModelData> getTree() {
		return tree;
	}

	public Button getBtnSetDefault() {
		return btnSetDefault;
	}

	public void setBtnSetDefault(Button btnSetDefault) {
		this.btnSetDefault = btnSetDefault;
	}

}
